﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace SchoolLibrary.DataBase.Connect
{
    public class MssqlConnection : Connection
    {
        private const string DATABASE_NAME = "school_library";
        private const string CONNECTION_STRING =
            @"Data Source=.\SQLEXPRESS;Initial Catalog={0};Integrated Security=True";

        private SqlConnection connection;

        public MssqlConnection()
        {
            string connectionString = String.Format(CONNECTION_STRING, DATABASE_NAME);
            connection = new SqlConnection(connectionString); 
        }

        public void Open()
        {
            connection.Open();
        }

        public DataTable executeSelect(string queryString)
        {
            DataSet dataSet = new DataSet();
            SqlDataAdapter dataAdapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand(queryString, connection);
            dataAdapter.SelectCommand = command;
            dataAdapter.Fill(dataSet);
            return dataSet.Tables[0];
        }

        public void executeUpdate(string queryString)
        {
            SqlDataAdapter dataAdapter = new SqlDataAdapter();
            SqlCommand updateCommand = new SqlCommand(queryString, connection);
            dataAdapter.UpdateCommand = updateCommand;
            dataAdapter.UpdateCommand.ExecuteNonQuery();
        }

        public void Close()
        {
            connection.Close();
        }
    }
}
